﻿<%
'后台子菜单
Function WindsPhoto_SubMenu(id)
	Dim aryName,aryPath,aryFloat,i
	If BlogUser.Level>1 Then
		aryName=Array("相册管理","新建相册","帮助说明","返回相册")
		aryPath=Array("admin_main.asp","admin_addtype.asp","help.asp",WP_SUB_DOMAIN)
		aryFloat=Array("m-left","m-left","m-right","m-right")
	Else
		aryName=Array("相册管理","新建相册","相册设置","帮助说明","更多功能","返回相册")
		aryPath=Array("admin_main.asp","admin_addtype.asp","admin_setting.asp","help.asp","help.asp#more.asp",WP_SUB_DOMAIN&""" target='_blank'")
		aryFloat=Array("m-left","m-left","m-left","m-right","m-right","m-right")
	End If
	For i=0 To Ubound(aryName)
		WindsPhoto_SubMenu=WindsPhoto_SubMenu & MakeSubMenu(aryName(i),aryPath(i),aryFloat(i)&IIf(i=id," m-now",""),False)
	Next
End Function

'首次安装数据库改名....
Function WindsPhoto_Add_Database()
	On Error Resume Next
	objConn.Execute "SELECT TOP 1 id FROM WINDSPHOTO_ZHUANTI"
	If Err.Number<>0 Then
		If ZC_MSSQL_ENABLE Then
			objConn.BeginTrans
			objConn.Execute "CREATE TABLE [WindsPhoto_zhuanti] ("&_
							"[id] int identity(1,1) not null primary key,"&_
							"[js] nvarchar(max),"&_
							"[hot] int,"&_
							"[pass] nvarchar(30),"&_
							"[view] smallint,"&_
							"[name] nvarchar(50),"&_
							"[ordered] int)"
			objConn.Execute "CREATE TABLE [WindsPhoto_desktop] ("&_
							"[id] int identity(1,1) not null primary key,"&_
							"[name] nvarchar(50),"&_
							"[zhuanti] smallint,"&_
							"[jj] nvarchar(max),"&_
							"[url] nvarchar(max),"&_
							"[surl] nvarchar(max),"&_
							"[hot] smallint,"&_
							"[itime] datetime,"&_
							"[viewnums] int"&_
							")"
			objConn.CommitTrans
		Else
			objConn.BeginTrans
			objConn.Execute "CREATE TABLE [WindsPhoto_desktop] ("&_
							"`id` AutoIncrement primary key,"&_
							"`name` VarChar(50),"&_
							"`zhuanti` Short,"&_
							"`jj` LongText,"&_
							"`url` LongText,"&_
							"`surl` LongText,"&_
							"`hot` Short,"&_
							"`itime` DateTime,"&_
							"`viewnums` Long"&_
							")"
			objConn.Execute "CREATE TABLE [WindsPhoto_zhuanti] ("&_
							"`id` AutoIncrement primary key,"&_
							"`js` LongText,"&_
							"`hot` Long,"&_
							"`pass` VarChar(30),"&_
							"`view` Short,"&_
							"`name` VarChar(50),"&_
							"`ordered` Long"&_
							")"
			objConn.CommitTrans
		End If
	End If
End Function

'添加链接到导航栏
Function WindsPhoto_Add_Navbar()
	Call GetFunction()
	Dim tmpstr
	tmpstr = Functions(FunctionMetas.GetValue("navbar")).Content
	If InStr(tmpstr,"相册")=0 or InStr(tmpstr,"windsphoto")=0 Then
		Functions(FunctionMetas.GetValue("navbar")).Content = tmpstr & "<li><a href=""<#ZC_BLOG_HOST#>zb_users/plugin/windsphoto/"">相册</a></li>"
		Functions(FunctionMetas.GetValue("navbar")).Save
	End If
	Call ClearGlobeCache
	Call LoadGlobeCache
End Function

'从导航栏删除链接
Function WindsPhoto_Del_Navbar()
	Call GetFunction()
	Functions(FunctionMetas.GetValue("navbar")).Content=RemoveLibyUrl(Functions(FunctionMetas.GetValue("navbar")).Content,"<#ZC_BLOG_HOST#>zb_users/plugin/windsphoto/")
	Functions(FunctionMetas.GetValue("navbar")).Save
	Call ClearGlobeCache
	Call LoadGlobeCache
End Function

'添加相册默认模板
Function WindsPhoto_Add_Template()
    Dim strContent
    strContent = LoadFromFile(BlogPath & "/zb_users/Theme/"& ZC_BLOG_THEME &"/"& ZC_TEMPLATE_DIRECTORY &"/page.html", "utf-8")
	strContent = Replace(strContent,"<#template:sidebar#>","<#template:sidebar4#>")
	If GetTemplate("TEMPLATE_WP_INDEX")=empty Then Call SaveToFile(BlogPath & "/zb_users/Theme/"& ZC_BLOG_THEME &"/"& ZC_TEMPLATE_DIRECTORY &"/wp_index.html", strContent, "utf-8", TRUE)
    If GetTemplate("TEMPLATE_WP_ALBUM")=empty Then Call SaveToFile(BlogPath & "/zb_users/Theme/"& ZC_BLOG_THEME &"/"& ZC_TEMPLATE_DIRECTORY &"/wp_album.html", strContent, "utf-8", TRUE)
	strContent = Replace(strContent,"<#template:sidebar4#>","<#template:sidebar5#>")
	If GetTemplate("TEMPLATE_WP_DISPLAY")=empty Then Call SaveToFile(BlogPath & "/zb_users/Theme/"& ZC_BLOG_THEME &"/"& ZC_TEMPLATE_DIRECTORY &"/wp_display.html", strContent, "utf-8", TRUE)
End Function

'获取图片上传目录
Function WindsPhoto_GetUploadFolder()
	Dim FilePath
	FilePath = "zb_users/plugin/windsphoto"
	If WP_UPLOAD_DIRBY = 1 Then
		CreatDirectoryByCustomDirectory(FilePath & "/" & WP_UPLOAD_DIR & "/" &Year(GetTime(Now()))&Month(GetTime(Now())))
		FilePath = FilePath & "/" & WP_UPLOAD_DIR & "/" &Year(GetTime(Now()))&Month(GetTime(Now())) & "/"
	ElseIf WP_UPLOAD_DIRBY = 2 Then
		CreatDirectoryByCustomDirectory(FilePath & "/" & WP_UPLOAD_DIR & "/" & zhuanti)
		FilePath = FilePath & "/" & WP_UPLOAD_DIR & "/" & zhuanti & "/"
	Else
		CreatDirectoryByCustomDirectory(FilePath & "/" & WP_UPLOAD_DIR)
		FilePath = FilePath & "/" & WP_UPLOAD_DIR & "/"
	End If
	WindsPhoto_GetUploadFolder=FilePath
End Function

'uEditor图片上传路径设置
Function WindsPhoto_ExportUEConfig(m)
	Call OpenConnect
	Call GetConfigs()
	Call WindsPhoto_Initialize
	If WP_BLOGPHOTO_ID = 0 Then Exit Function
	Dim j,k,tmp
	j=Split(m,"imagePath:""")(0)
	k=Split(m,""",imageFieldName:")(1)
	tmp=BlogHost & WindsPhoto_GetUploadFolder
	m=j&"imagePath:"""&tmp&""",imageFieldName:"&k
End Function

'uEditor图片上传
Function WindsPhoto_uEditorUpload()
	'On Error Resume Next
	Call WindsPhoto_Initialize
	If WP_BLOGPHOTO_ID = 0 Then Exit Function
	If Instr(Request.ServerVariables("URL"),"imageUp.asp") Then

		Dim upload,FilePath,formname,zhuanti
		FilePath=WindsPhoto_GetUploadFolder
		FilePath=BlogPath & Replace(FilePath,"/","\")
		formname="edtFileLoad"
		zhuanti=WP_BLOGPHOTO_ID
		Set upload=New UpLoadClass
		upload.AutoSave=2
		upload.Charset="UTF-8"
		upload.FileType=Replace(ZC_UPLOAD_FILETYPE,"|","/")
		upload.savepath=FilePath
		upload.maxsize=ZC_UPLOAD_FILESIZE
		upload.open
		Dim FileExt,Filename,FileNamet,FileNamelen,FileNamet1,imgWidth,imgHeight,autoname,name,url,surl
		FileExt = LCase(upload.form(formname&"_Ext"))
		name= Replace(upload.Form(formname&"_Name"), "."&FileExt, "")
		If WP_UPLOAD_RENAME=True Then autoname=0 Else autoname=1
		If upload.Save(formname,autoname)=True Then
			Filename=FilePath & upload.form(formname)
			FileNamet=upload.form(formname)
			If WP_IF_ASPJPEG=True Then
				Dim Jpeg
				Set Jpeg = Server.CreateObject("Persits.Jpeg")
				'如果aspjpeg版本大于1.9，保护Metadata
				If Jpeg.Version>= "1.9" Then Jpeg.PreserveMetadata = True
				Jpeg.Open FileName
				'变更缩略图文件扩展名为jpg
				FileNamelen = Len(FileNamet) - 4
				FileNamet1 = FileNamet
				FileNamet = Left(FileNamet, FileNamelen) &".jpg"
				imgWidth = Jpeg.OriginalWidth
				imgHeight = Jpeg.OriginalHeight

				'缩放图片
				If WP_JPEG_MAXWIDTH = False And imgWidth>WP_JPEG_WIDTH Then
					Jpeg.PreserveAspectRatio = True
					Jpeg.width = WP_JPEG_WIDTH
				End If
				Jpeg.Quality = 100
				Jpeg.Save FileName

				If WP_THUMB_TYPE = 1 Then
					'缩略图处理，以长边保持比例进行缩放
					If imgWidth>= imgHeight And imgWidth>WP_SMALL_WIDTH Then
						Jpeg.Width = WP_SMALL_WIDTH
						Jpeg.Height = imgHeight / (imgWidth / WP_SMALL_WIDTH)
					End If
					If imgHeight>imgWidth And imgHeight>WP_SMALL_HEIGHT Then
						Jpeg.Height = WP_SMALL_HEIGHT
						Jpeg.Width = imgWidth / (imgHeight / WP_SMALL_HEIGHT)
					End If
				Else
					'以短边进行裁剪缩放
					If (imgWidth/imgHeight)>=(WP_SMALL_WIDTH/WP_SMALL_HEIGHT) Then
						nwidth=CInt((imgWidth/imgHeight)*WP_SMALL_HEIGHT)
						nheight=WP_SMALL_HEIGHT
						Jpeg.Width=nwidth
						Jpeg.Height=nheight
						x1=int((nwidth-WP_SMALL_WIDTH)/2):y1=0:x2=x1+WP_SMALL_WIDTH:y2=WP_SMALL_HEIGHT
					Else
						nwidth=WP_SMALL_WIDTH
						nheight=CInt((imgHeight/imgWidth)*WP_SMALL_WIDTH)
						Jpeg.Width=nwidth
						Jpeg.Height=nheight
						x1=0:y1=int((nheight-WP_SMALL_HEIGHT)/2):x2=WP_SMALL_WIDTH:y2=y1+WP_SMALL_HEIGHT
					End If
					Jpeg.Crop x1,y1,x2,y2
				End If
				'保存缩略图，并进行微度锐化
				Jpeg.Sharpen 1, 110
				Jpeg.Save FilePath & "small_" & FileNamet
				Jpeg.Close

				If WP_WATERMARK_AUTO=True And FileExt<>"gif" Then
					Dim TextWidth,PositionWidth,PositionHeight
					Jpeg.Open FileName
					'渐进式JPEG
					Jpeg.Progressive = True
					Jpeg.Quality = WP_JPEG_QUALITY
					If WP_WATERMARK_TYPE = "1" Then '文字水印
						Jpeg.Canvas.Font.Color = Replace(WP_JPEG_FONTCOLOR, "#", "&h") '字体颜色
						Jpeg.Canvas.Font.Family = WP_JPEG_FONTFAMILY 'family设置字体
						Jpeg.Canvas.Font.Bold = WP_JPEG_FONTBOLD '是否设置成粗体
						Jpeg.Canvas.Font.Italic = WP_JPEG_FONTITALIC '是否斜体
						Jpeg.Canvas.Font.Size = WP_JPEG_FONTSIZE '字体大小
						Jpeg.Canvas.Font.Quality = WP_JPEG_FONTQUALITY '文字质量
						TextWidth = Jpeg.Canvas.GetTextExtent(WP_WATERMARK_TEXT)
						Select Case WP_WATERMARK_WIDTH_POSITION
							Case "left"
								PositionWidth = 10
							Case "center"
								PositionWidth = (Jpeg.Width - TextWidth) / 2
							Case "right"
								PositionWidth = Jpeg.Width - TextWidth - 10
						End Select
						Select Case WP_WATERMARK_HEIGHT_POSITION
							Case "top"
								PositionHeight = 10
							Case "center"
								PositionHeight = (Jpeg.Height - 12) / 2
							Case "bottom"
								PositionHeight = Jpeg.Height - 12 - 10
						End Select
						Jpeg.Canvas.Print PositionWidth, PositionHeight, WP_WATERMARK_TEXT
						Jpeg.Save FileName
					ElseIf WP_WATERMARK_TYPE = "2" Then 'logo水印
						Dim Logo
						Set Logo = Server.CreateObject("Persits.Jpeg")
						WP_WATERMARK_LOGO = Server.MapPath("/") & "\zb_users\plugin\windsphoto\" & WP_WATERMARK_LOGO
						Logo.Open WP_WATERMARK_LOGO
						Select Case WP_WATERMARK_WIDTH_POSITION
							Case "left"
								PositionWidth = 10
							Case "center"
								PositionWidth = (Jpeg.Width - Logo.Width) / 2
							Case "right"
								PositionWidth = Jpeg.Width - Logo.Width - 10
						End Select
						Select Case WP_WATERMARK_HEIGHT_POSITION
							Case "top"
								PositionHeight = 10
							Case "center"
								PositionHeight = (Jpeg.Height - Logo.Height) / 2
							Case "bottom"
								PositionHeight = Jpeg.Height - Logo.Height - 10
						End Select
						If Instr(WP_WATERMARK_LOGO,"png") Or Instr(WP_WATERMARK_LOGO,"gif") And Jpeg.Version >= "1.8" Then
							Jpeg.Canvas.DrawPNG PositionWidth, PositionHeight, WP_WATERMARK_LOGO
						Else
							Jpeg.Canvas.DrawImage PositionWidth, PositionHeight, Logo, WP_WATERMARK_ALPHA
						End If
						Jpeg.Save FileName
						Set Logo = Nothing
					End If
				End If
				Jpeg.Close
				Set Jpeg = Nothing

			End If

			If WP_IF_ASPJPEG=True Then
				'带缩略图的URL路径
				If WP_UPLOAD_DIRBY = 1 Then
					url = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/" & FileNamet1
					surl = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/small_" & FileNamet
				ElseIf WP_UPLOAD_DIRBY = 2 Then
					url = WP_UPLOAD_DIR & "/" & zhuanti & "/" & FileNamet1
					surl = WP_UPLOAD_DIR & "/" & zhuanti & "/small_" & FileNamet
				Else
					url = WP_UPLOAD_DIR & "/" & FileNamet1
					surl = WP_UPLOAD_DIR & "/small_" & FileNamet
				End If
			Else
				'不带缩略图的URL路径
				If WP_UPLOAD_DIRBY = 1 Then
					url = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/" & FileNamet
					surl = WP_UPLOAD_DIR & "/" & Year(GetTime(Now()))&Month(GetTime(Now())) & "/" & FileNamet
				ElseIf WP_UPLOAD_DIRBY = 2 Then
					url = WP_UPLOAD_DIR & "/" & zhuanti & "/" & FileNamet
					surl = WP_UPLOAD_DIR & "/" & zhuanti & "/" & FileNamet
				Else
					url = WP_UPLOAD_DIR & "/" & FileNamet
					surl = WP_UPLOAD_DIR & "/" & FileNamet
				End If
			End If

			'写入数据库
			objConn.Execute "insert into WindsPhoto_desktop ([name],[itime],zhuanti,url,surl,hot,viewnums) values ('"&name&"','"&now()&"',"&zhuanti&",'"&url&"','"&surl&"','0','0')"

		End If

		Dim strJSON
		strJSON="{'state':'"& upload.Error2Info("edtFileLoad") & "','url':'"& upload.form("edtFileLoad") &"','fileType':'"&upload.form("edtFileLoad_Ext")&"','title':'"&name&"','original':'"&upload.Form("edtFileLoad_Name")&"'}"

		For Each sAction_Plugin_uEditor_FileUpload_End in Action_Plugin_uEditor_FileUpload_End
			If Not IsEmpty(sAction_Plugin_uEditor_FileUpload_End) Then Call Execute(sAction_Plugin_uEditor_FileUpload_End)
		Next
		response.AddHeader "json",strjson
		response.write strJSON

		Set upload=Nothing

		Response.End

	End If

End Function

'uEditor图片管理
Function WindsPhoto_uEditorAlbumList()
	Call WindsPhoto_Initialize
	If WP_BLOGPHOTO_ID = 0 Then Exit Function
	Dim objRs,i
	Set objRS=Server.CreateObject("ADODB.Recordset")
	objRS.CursorType = adOpenKeyset
	objRS.LockType = adLockReadOnly
	objRS.ActiveConnection=objConn
	objRS.Source=""
	objRS.Open("SELECT TOP 100 url FROM WindsPhoto_desktop WHERE url NOT LIKE '%http%' ORDER BY id DESC")
	objRS.PageSize=ZC_MANAGE_COUNT
	Call CheckParameter(intPage,"int",1)
	If objRS.PageCount>0 Then objRS.AbsolutePage = intPage
	intPageAll=objRS.PageCount
	If (Not objRS.bof) And (Not objRS.eof) Then
		For i=1 to objRS.PageSize
			Response.Write Replace("zb_users/plugin/windsphoto/"&objRS("url")&uEditor_Split,"%","%25")
			objRS.MoveNext
			If objRS.Eof Then Exit For
		Next
	End If
	Set objRS = Nothing
	Response.End
End Function

'同步到腾讯微博
Function WindsPhoto_syn_tqq
	init_qqconnect()
	Dim objRS,t_add,t_add_,ID,content,link
	Set objRS=objConn.Execute("SELECT MAX([ID]) FROM [WindsPhoto_desktop]")
	If (Not objRS.bof) And (Not objRS.eof) Then ID=objRS(0)
	Set objRS=Nothing
	If WP_SCRIPT_TYPE = 4 Then
		link = WP_SUB_DOMAIN&"display.asp?id="&ID
	Else
		link = WP_SUB_DOMAIN&"album.asp?typeid="&zhuanti
	End If
	If Left(url,4)<>"http" Then url = WP_SUB_DOMAIN&url
	content = "发布了图片："&name&"，"&link
	t_add = qqconnect.t.t(content,url)
	Set t_add_ = qqconnect.functions.json.toObject(t_add)
	If t_add_.ret = 0 Then SetBlogHint_Custom("恭喜,同步到腾讯微博成功.") Else SetBlogHint_Custom("同步到腾讯微博出现问题.")
End Function

'保存侧栏模块
Function WindsPhoto_SaveFunctions(id,name,htmlid,ftype,viewtype,content)
	GetFunction()
	Dim objfunction
	Set objfunction=New TFunction
	If FunctionMetas.GetValue(id)="" Then
		objfunction.ID=0
		objfunction.Name=name
		objfunction.FileName=id
		objfunction.HtmlID=htmlid
		objfunction.Ftype=ftype
		objfunction.Order=0
		objfunction.SidebarID=0
		If Right(ZC_BLOG_VERSION,6)>121221 Then
			objfunction.ViewType=viewtype
			objFunction.Source="plugin_WindsPhoto"
		End If
		objfunction.isSystem=False
		objfunction.Content=content
		objfunction.Save
	Else
		Functions(FunctionMetas.GetValue(id)).Content=content
		Functions(FunctionMetas.GetValue(id)).Post()
		Functions(FunctionMetas.GetValue(id)).SaveFile
	End If
End Function

'删除侧栏模块
Function WindsPhoto_DelFunctions(id)
	GetFunction()
	Dim objfunction
	Set objfunction=New TFunction
	objfunction.ID=FunctionMetas.GetValue(id)
	objfunction.FileName=id
	objfunction.Del
End Function

Function WindsPhoto_SaveAlbumlist()
    GetFunction()
	Dim objRs,objCountRs,count,i,j,strAlbumlist
	i=Functions(FunctionMetas.GetValue("wp_albumlist")).MaxLi
	If i=0 Then i=10
	j=0
	Set objRS=objConn.Execute("select * FROM WindsPhoto_zhuanti where pass='' order by ordered,id asc")
    Do While Not objRS.EOF
		Set objCountRs=objConn.Execute("select count(*) as C FROM WindsPhoto_desktop where zhuanti="&objRS("id"))
        count = objCountRs("C")
        strAlbumlist = strAlbumlist&"<li><a href="""& WP_SUB_DOMAIN &"album.asp?typeid="&objRS("id")&""">"&objRS("name")&"<span class=""article-nums""> ("&count&")</span></a></li>" & VBCRLF
        objRS.MoveNext
		j=j+1
		If j=i Then Exit Do
    Loop
	Call WindsPhoto_SaveFunctions("wp_albumlist","相册分类","divWP_Albumlist","ul","",strAlbumlist)
End Function

Function WindsPhoto_SaveNewestlist()
	Dim objRs,objPassRs,pass,i,j,strNewestlist,surl
	Set objPassRs=objConn.Execute("Select * From WindsPhoto_zhuanti Where pass<>''")
	pass = 0
	Do While Not objPassRs.EOF
		pass = pass &","& objPassRs("id")
		objPassRs.movenext
	Loop

	Call GetFunction()
	i=Functions(FunctionMetas.GetValue("wp_newestlist")).MaxLi
	If i=0 Then i=8
	j=0
	Set objRs = objConn.Execute("Select * From WindsPhoto_desktop Where zhuanti not in ("&pass&") order by id desc")
	Do While Not objRS.EOF
		surl = objRs("surl")
		If Left(surl, 4)<>"http" Then surl = WP_SUB_DOMAIN & surl
        strNewestlist = strNewestlist&"<li><a href="""& WP_SUB_DOMAIN &"display.asp?id="&objRs("id")&""" title="""&objRs("name")&"""><img src="""&surl&""" alt="""&objRs("name")&""" /><span>"&objRs("name")&"</span></a></li>" & VBCRLF
        objRs.movenext
		j=j+1
		If j=i Then Exit Do
    Loop
	Call WindsPhoto_SaveFunctions("wp_newestlist","相册最新图片","divWP_Newestlist","ul","",strNewestlist)
End Function
%>